package com.singlever;


import com.ChessUtils.ChessUtil;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;


/**
 * @author acoffee
 * @create 2021-08-12 19:12
 */
public class SingleGoBangView extends Stage {
    // 场景的长度
    private int SceneLength = 590;
    // 棋盘的长宽（我们这里设为正方形所以长宽一样）
    private int ChessboardLength = 520;
    // 棋盘格子的长度（我们这里设为正方形所以长宽一样）
    private int PaneLength = 40;
    //定义按钮的参数
    public int Btn_width = 72, Btn_height = 40, ReActionBtn_Size = 32;


    public SingleGoBangView() {

        setTitle("Acoffee的五子棋");

        //窗体不能修改
        setResizable(false);

        //获取画板对象
        Pane pane = getPane();

        //创建场景对象，并将画板放到场景上
        Scene scene = new Scene(pane, SceneLength, SceneLength + PaneLength);

        //将场景设置到舞台上
        setScene(scene);
    }

    //创建画板，设置画板的各种模块
    public Pane getPane() {

        //创建画板对象
        Pane pane = new Pane();

        //创建一个功能对象
        SingleButtonService playaction = new SingleButtonService();

        //落子
        playaction.Click(this,pane, PaneLength, SceneLength);

        //给画板对象设置背景颜色
        pane.setBackground(new Background(new BackgroundImage(new Image("/background.jpg"), null, null, null, null)));

        //创建线条对象
        paintLine(pane);

        //添加按钮
        addBtn(pane, playaction);

        return pane;
    }

    //绘制棋盘线条
    public void paintLine(Pane pane) {
        for (int i = 0; i < ChessboardLength + PaneLength / 2; i += PaneLength) {
            //画棋盘上的横线
            Line linex = new Line(PaneLength, PaneLength + i, ChessboardLength + PaneLength, PaneLength + i);
            pane.getChildren().add(linex);
            //画棋盘上的竖线
            Line liney = new Line(PaneLength + i, PaneLength, PaneLength + i, ChessboardLength + PaneLength);
            pane.getChildren().add(liney);
        }
    }

    //添加按钮
    public void addBtn(Pane pane, SingleButtonService playaction) {
        //新局按钮
        Button NewGameBtn = ChessUtil.getButton("新局", Btn_width, Btn_height, PaneLength, PaneLength * 1.5 + ChessboardLength);
        //保存按钮
        Button SaveGameBtn = ChessUtil.getButton("保存", Btn_width, Btn_height, PaneLength * 2 + Btn_width, PaneLength * 1.5 + ChessboardLength);
        //打谱按钮
        Button ReGameBtn = ChessUtil.getButton("打谱", Btn_width, Btn_height, PaneLength * 3 + Btn_width * 2, PaneLength * 1.5 + ChessboardLength);
        //悔棋按钮
        Button RegretBtn = ChessUtil.getButton("悔棋", Btn_width, Btn_height, PaneLength * 4 + Btn_width * 3, PaneLength * 1.5 + ChessboardLength);
        //退出按钮
        Button ExitBtn = ChessUtil.getButton("退出", Btn_width, Btn_height, PaneLength * 5 + Btn_width * 4, PaneLength * 1.5 + ChessboardLength);

        //上一步按钮
        Button btnBack = ChessUtil.getButton("<", ReActionBtn_Size, ReActionBtn_Size, PaneLength, 3);
        //下一步按钮
        Button btnNext = ChessUtil.getButton(">", ReActionBtn_Size, ReActionBtn_Size, PaneLength * 2.5, 3);
        //退出打谱按钮
        Button btnRegameExit = ChessUtil.getButton("X", ReActionBtn_Size, ReActionBtn_Size, PaneLength * 4, 3);

        //为新局按钮添加功能
        playaction.newGameButtonAction(this,NewGameBtn, pane, btnBack, btnNext, btnRegameExit);
        //为保存按钮添加功能
        playaction.SaveGameBtnAction(SaveGameBtn, this);
        //为打谱按钮添加功能
        playaction.ReGameBtnAction(this, ReGameBtn, pane, btnBack, btnNext, btnRegameExit, PaneLength);
        //为悔棋按钮添加功能
        playaction.RegretBtnAction(pane, RegretBtn);
        //为退出按钮添加功能
        playaction.ExitBtnAcition(this,ExitBtn);

        //将组件添加到棋盘上
        pane.getChildren().addAll(NewGameBtn, SaveGameBtn, ReGameBtn, RegretBtn, ExitBtn);
    }

}
